﻿using System;
using System.Collections.Generic;
using System.Linq;
using Common;
using WqLunTan.ConstConfig;
using WqLunTan.DBContext;
using WqLunTan.Models.DataBase;
using WqLunTan.Models.ViewModel;

namespace WqLunTan.BLL
{
    /// <summary>
    /// 查询数据库,主要用于执行模板页的查询
    /// </summary>
    public class LayoutDataServices
    {

        private readonly MySqlDbContext dbContext;

        public LayoutDataServices(MySqlDbContext _dbContext)
        {
            dbContext = _dbContext;
        }

        /// <summary>
        /// 模板页获取show=true的公告
        /// </summary>
        /// <returns></returns>
        public notice getNotice()
        {
            var temp = httpCacheHelper.getCache<notice>("notice_layout");
            if (temp != null && !string.IsNullOrWhiteSpace(temp.content))
            {
                return temp;     // 读取缓存
            }

            var notice = dbContext.notice.Where(x => x.is_show == true).OrderByDescending(x => x.create_time).FirstOrDefault();
            httpCacheHelper.setCache("notice_layout", notice, 10);
            return notice;
        }

        /// <summary>
        /// 获取前10条帖子, 置顶->排序->状态(显示优先)->添加时间 逆序
        /// </summary>
        /// <returns></returns>
        public List<TitleUserExt> getPostTitleList()
        {
            var temp = httpCacheHelper.getCache<List<TitleUserExt>>("post_list_layout");
            if (temp != null && temp.Count > 0)
            {
                return temp;     // 读取缓存
            }

            var q = from t in dbContext.PostTitle
                    join u in dbContext.Users on t.uid equals u.Id
                    where t.is_show == true
                    orderby t.top descending, t.sort_num ascending, t.create_time descending
                    select new TitleUserExt()
                    {
                        id = t.id,
                        create_time = t.create_time,
                        title = t.title,
                        keywords = t.keywords,
                        nick_name = u.NickName,
                        sort_num = t.sort_num,
                        is_show = t.is_show,
                        top = t.top,
                        uid = t.uid
                    };
            var psot_list = q.Take(10).ToList();
            httpCacheHelper.setCache("post_list_layout", psot_list, 10);

            return psot_list;

        }

        /// <summary>
        /// 获取模板页布局的内容
        /// </summary>
        /// <returns></returns>
        public List<site_layout_content> getAdContent()
        {
            var temp = httpCacheHelper.getCache<List<site_layout_content>>("site_header_list_layout");
            if (temp != null && temp.Count > 0)
            {
                return temp;     // 读取缓存
            }
            var list = dbContext.SiteHeader.ToList();
            httpCacheHelper.setCache("site_header_list_layout", list, 10);

            return list;
        }

        /// <summary>
        /// 获取文章分类列表的名称, 默认的6个分类之外
        /// </summary>
        /// <returns></returns>
        public List<article_category> getCategoryListOther()
        {
            var temp = httpCacheHelper.getCache<List<article_category>>("catelist_layout_banner");
            if (temp != null && temp.Count > 0)
            {
                return temp;     // 读取缓存
            }

            var list = dbContext.art_category.OrderBy(x => x.sort).ThenBy(x => x.create_time).Skip(6).Take(10).ToList();
            httpCacheHelper.setCache("catelist_layout_banner", list, 10);
            return list;
        }

        /// <summary>
        /// 获取文章分类的内容
        /// </summary>
        /// <returns></returns>
        public List<CategoryAricleExt> getCateArticleDict()
        {
            var temp = httpCacheHelper.getCache<List<CategoryAricleExt>>("cate_article_dict_layout");
            if (temp != null && temp.Count > 0)
            {
                return temp;     // 读取缓存
            }

            // 文章分类列表
            var categories = dbContext.art_category.Where(x => x.is_show).
                OrderBy(x => x.sort).ThenBy(x => x.id).
                Select(x => new CategoryAricleExt() { id = x.id, category_name = x.category_name, img = x.img, sort = x.sort }).
                Take(7)
                .ToList();
            if (categories != null && categories.Count > 0)
            {
                categories.ForEach(item =>
                {
                    var artlist = dbContext.article.Where(x => x.cateid == item.id && x.is_show).OrderByDescending(x => x.top).ThenBy(x => x.sort_num).ThenByDescending(x => x.create_time).Take(10).ToList();
                    item.artlist = artlist;
                });
            }
            httpCacheHelper.setCache("cate_article_dict_layout", categories, 10);

            return categories;
        }

        public string getSiteTitle()
        {
            var siteTitleKey = ConfigKey.SiteTitle;

            var config = httpCacheHelper.getCache<site_config>(siteTitleKey);
            if (config == null)
            {
                config = dbContext.SiteConfig.Where(x => x.config_key.Equals(siteTitleKey)).SingleOrDefault() ?? new site_config();
                httpCacheHelper.setCache(siteTitleKey, config, 10);
            }
            if (string.IsNullOrWhiteSpace(config.config_value))
            {
                config.config_value = "魏清八字风水算命论坛";
            }

            return config.config_value;
        }

    }
}
